package com.peng.kslc;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A5LongestPalindrome
 * https://ssg.leetcode-cn.com/problems/longest-palindromic-substring/
 *
 * @author lupeng
 * Created on 2022-07-24
 */
public class A5LongestPalindrome {

    private static final Logger logger = LoggerFactory.getLogger(A5LongestPalindrome.class);

    public static void main(String[] args) {
        System.out.println(new A5LongestPalindrome().longestPalindrome("a"));
    }

    public String longestPalindrome(String s) {
        String maxLenStr = "";
        Map<Integer, String> len2StrMap = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            String len1 = expendAroundCenter(s, i, i);
            String len2 = expendAroundCenter(s, i, i + 1);
            String len = len1.length() >= len2.length() ? len1 : len2;
            if (len.length() > maxLenStr.length()) {
                maxLenStr = len;
            }
        }
        return maxLenStr;
    }

    private String expendAroundCenter(String s, int left, int right) {
        while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
            left--;
            right++;
        }
        return s.substring(left + 1, right);
    }
}
